package lily.phs.entertain;

import lily.phs.urh.R;
import lily.ui.bookshelf.ImageUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MediaMainActivity extends Activity{
//	private ListView listview=null;
	private Intent intent = null;
//	private static final int ITEM1 = Menu.FIRST;
//	private static final int ITEM2 = Menu.FIRST+1;
	private ScanSdReceiver scanSdReceiver = null;
	
	private static final String[] arrayList = {"全部音乐","最近播放列表","最经常播放列表","返回"};
	private static final int[] imageList={R.drawable.music_all,
		R.drawable.music_recely,R.drawable.music_often,R.drawable.moveback

		};
	private TextView tv_TitleBar;
	private GridView gvMedia;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		this.setContentView(R.layout.media);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
		
		tv_TitleBar = (TextView)this.findViewById(R.id.title);
		tv_TitleBar.setText("音乐播放器");
		Button btBack = (Button)this.findViewById(R.id.title_left_btn);
		btBack.setText("后退");
		btBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
			}
		});
		Button btSave = (Button)this.findViewById(R.id.title_right_btn);
		btSave.setVisibility(View.GONE);
		Button btSubmit = (Button)this.findViewById(R.id.title_right_btn2);
		btSubmit.setVisibility(View.GONE);
		
		gvMedia = (GridView)this.findViewById(R.id.grid_media);
		SquareItemAdapter adapter = new SquareItemAdapter(this);
		gvMedia.setAdapter(adapter);
		gvMedia.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
			        intent = new Intent();
					intent.setClass(MediaMainActivity.this, MediaListActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
//					finish();
					break;

				case 1:
					/*intent = new Intent();
					intent.setClass(MediaMainActivity.this, RecentlyActivity.class);
					startActivity(intent);
					finish();*/
					break;
				case 2:
					/*intent = new Intent();
					//intent.setClass(MainActivity.this, ClicksActivity.class);
					startActivity(intent);
					finish();*/
					break;
				case 3:{
					finish();
					overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
					break;
				}
				}
				
			}
			
		});
	}
	
	
	 
	class SquareItemAdapter extends BaseAdapter{
		Context context;
		TextView tvName;

		public SquareItemAdapter(Context arg2){
			this.context = arg2;
		}

		@Override
		public int getCount(){
			return 4;
		}

		@Override
		public Object getItem(int paramInt){
			return Integer.valueOf(paramInt);
		}

		@Override
		public long getItemId(int paramInt){
			return paramInt;
		}

		@Override
		public View getView(int paramInt, View paramView, ViewGroup paramViewGroup){
			
			LinearLayout local = new LinearLayout(context);
			local.setOrientation(LinearLayout.VERTICAL);   
	        local.setGravity(Gravity.CENTER);
	        
			TextView tv = new TextView(context);
			ImageView iv=new ImageView(context);
			
			tv.setTextColor(Color.CYAN);
			tv.setTextSize(22.0f);
			
			if (paramInt == 0){
				String str1 = arrayList[0];
				tv.setText(str1);
				local.setBackgroundResource(R.drawable.circle_list_top);
				Drawable db=this.context.getResources().getDrawable(imageList[0]);
		        ImageUtil iu=new ImageUtil();
		        Bitmap bmp=iu.drawableToBitmap(db);
		        bmp=iu.zoomBitmap(bmp, 100, 100);
		        bmp=iu.getRoundedCornerBitmap(bmp, 10.0f);
		        iv.setImageBitmap(bmp);
				//iv.setBackgroundResource(R.drawable.chufang_zhifa);
			}

			if (paramInt==1){
				String str2 = arrayList[paramInt];
				tv.setText(str2);
				local.setBackgroundResource(R.drawable.circle_list_top);
				Drawable db=this.context.getResources().getDrawable(imageList[paramInt]);
		        ImageUtil iu=new ImageUtil();
		        Bitmap bmp=iu.drawableToBitmap(db);
		        bmp=iu.zoomBitmap(bmp, 100, 100);
		        bmp=iu.getRoundedCornerBitmap(bmp, 10.0f);
		        iv.setImageBitmap(bmp);
			}
			
			if(paramInt == 2){
				String str3 = arrayList[2];
				tv.setText(str3);
				local.setBackgroundResource(R.drawable.circle_list_bottom);	
				Drawable db=this.context.getResources().getDrawable(imageList[2]);
		        ImageUtil iu=new ImageUtil();
		        Bitmap bmp=iu.drawableToBitmap(db);
		        bmp=iu.zoomBitmap(bmp, 100, 100);
		        bmp=iu.getRoundedCornerBitmap(bmp, 10.0f);
		        iv.setImageBitmap(bmp);
			}
			
			if(paramInt == 3){
				String str3 = arrayList[3];
				tv.setText(str3);
				local.setBackgroundResource(R.drawable.circle_list_bottom);	
				Drawable db=this.context.getResources().getDrawable(imageList[3]);
		        ImageUtil iu=new ImageUtil();
		        Bitmap bmp=iu.drawableToBitmap(db);
		        bmp=iu.zoomBitmap(bmp, 100, 100);
		        bmp=iu.getRoundedCornerBitmap(bmp, 10.0f);
		        iv.setImageBitmap(bmp);
			}

			LayoutParams para = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			local.addView(iv,para);
			local.addView(tv,para);
			return local;
		}
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == event.KEYCODE_BACK) {
			if (scanSdReceiver!=null)
      				unregisterReceiver(scanSdReceiver);
			Intent intent = new Intent();
			intent.setClass(this, YuleActivity.class);
			startActivity(intent);
			finish();
		}
		return true;
    }
}
